<?php
class Bugs extends Controller{
    function Bugs()
    {
        parent::Controller();
    
    }
    
    function index()
    {
        if($this->userland->logged_in())
        {
            $data = array(
                            'user_logged_in'=>true
                    );
        }
        else 
        {
            $data = array(
                            'user_logged_in'=>false
                    );
        }
        $this->load->view('bugs/show_form', $data);
    }
    
	function bug_list()
	{
		if (!$this->userland->logged_in() || !$this->userland->current_user()->is_admin)
		{
			show_404();
			exit();
		}
		
		$this->load->database();
		$this->load->helper('date');
		
		$data['bugs'] = $this->db->query('select * from bugs order by created_at desc');
		$this->load->view('bugs/list', $data);
	}
	
	function clear_all()
	{
		if (!$this->userland->logged_in() || !$this->userland->current_user()->is_admin)
		{
			show_404();
			exit();
		}
		$this->db->query('delete from bugs');
		header('Location: /bugs/bug_list');
	}

    function report()
    {        
        $this->load->database();  //< initialize database
        
		$email = "";
        if($this->input->post('email', true))
        {
            $email = $this->input->post('email', true);
        }
        else if($this->userland->logged_in()){
            $user  = $this->userland->current_user();
            $email = $user->email;
        }
        
        $report_description = $this->input->post('report', true);
        if(empty($report_description))
        {
            //no bug desription throw error for user
            $this->flashy->save_flash('error', 'Please include a bug description.');
        }

		if(empty($email))
        {
            //no email throw error for user
            $this->flashy->save_flash('error', 'Please include your email address.');
            
        }

		if (empty($email) || empty($report_description))
		{
			$data['email'] 	  = $email;
			$data['bug_desc'] = $report_description;
			$data['user_logged_in'] = $this->userland->logged_in();
			$this->load->view('bugs/show_form', $data);
		}
		else
		{
	        //process form request and save
	        $submitter_data = array(
	                                    'ip'     => $this->input->ip_address(),
	                                    'agent'  => $this->input->user_agent(),
	                                    'email'  => $email,
										'uri'	 => $this->input->post('referer', true)
	                                   );
	       $serialized_data = serialize($submitter_data);
	       $query = $this->db->query('insert into bugs (report, data, created_at, updated_at) 
	                                  values(?, ?, NOW(), NOW())', array($report_description, $serialized_data));
	
			// email kyle
			$this->emailer->deliver_bug_report(array(
				'to'		=> 'kyle.bragger+bricabugs@gmail.com',
				'subject'	=> "Bug report submitted",
				'data'		=> array(
					'data' => array(
						'data'   => print_r($submitter_data, true),
						'report' => $report_description
					)
				)
			));

	       if($query)
	       {
	            $this->flashy->save_flash('notice', 'Your bug was successfully submitted. Thank you!');
	       }
	       else 
	       {
	          $this->flashy->save_flash('error', 'Bug was not successfuly saved.');
	       }
	       header('Location: /bugs');
	       exit;
		}
    }

}
?>
